{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1599819467604",
   "display_name": "Python 3.6.3 64-bit ('python3.6': virtualenv)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import linalg as la\n",
    "from numpy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loadExData3():\n",
    "    # 利用SVD提高推荐效果，菜肴矩阵\n",
    "    return[[2, 0, 0, 4, 4, 0, 0, 0, 0, 0, 0],\n",
    "           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5],\n",
    "           [0, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0],\n",
    "           [3, 3, 4, 0, 3, 0, 0, 2, 2, 0, 0],\n",
    "           [5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0],\n",
    "           [0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 0],\n",
    "           [4, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5],\n",
    "           [0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4],\n",
    "           [0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 0],\n",
    "           [0, 0, 0, 3, 0, 0, 0, 0, 4, 5, 0],\n",
    "           [1, 1, 2, 1, 1, 2, 1, 0, 4, 5, 0]]\n",
    "\n",
    "myMat = mat(loadExData3())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "matrix([[2, 0, 0, 4, 4, 0, 0, 0, 0, 0, 0],\n        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5],\n        [0, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0],\n        [3, 3, 4, 0, 3, 0, 0, 2, 2, 0, 0],\n        [5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0],\n        [0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 0],\n        [4, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5],\n        [0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4],\n        [0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 0],\n        [0, 0, 0, 3, 0, 0, 0, 0, 4, 5, 0],\n        [1, 1, 2, 1, 1, 2, 1, 0, 4, 5, 0]])"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "myMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recommend(dataMat, user, N=3, simMeas=cosSim, estMethod=standEst):\n",
    "    \"\"\"svdEst( )\n",
    "    Args:\n",
    "        dataMat         训练数据集\n",
    "        user            用户编号\n",
    "        simMeas         相似度计算方法\n",
    "        estMethod       使用的推荐算法\n",
    "    Returns:\n",
    "        返回最终 N 个推荐结果\n",
    "    \"\"\"\n",
    "    # 寻找未评级的物品\n",
    "    # 对给定的用户建立一个未评分的物品列表\n",
    "    \n",
    "    unratedItems = nonzero(dataMat[user, :].A == 0)[1]\n",
    "    # 如果不存在未评分物品，那么就退出函数\n",
    "    if len(unratedItems) == 0:\n",
    "        return 'you rated everything'\n",
    "    # 物品的编号和评分值\n",
    "    itemScores = []\n",
    "    # 在未评分物品上进行循环\n",
    "    for item in unratedItems:\n",
    "        # 获取 item 该物品的评分\n",
    "        estimatedScore = estMethod(dataMat, user, simMeas, item)\n",
    "        itemScores.append((item, estimatedScore))\n",
    "    # 按照评分得分 进行逆排序，获取前N个未评级物品进行推荐\n",
    "    return sorted(itemScores, key=lambda jj: jj[1], reverse=True)[: N]\n",
    "\n",
    "\n",
    "print(recommend(myMat, 1, estMethod=svdEst))"
   ]
  }
 ]
}